import http from 'node:http'
import url from 'node:url'
import express from 'express'
/* /http */
const router = express.Router() //路由模块

router.get('/:id', (req, res) => {
  console.log(req.params)
  let id = req.params.id || 98
  http
    .createServer((req, res) => {
      res.setHeader('Content-Type', 'application/json') // 设置响应头的 Content-Type 为 'application/json'
      res.statusCode = 200 // 设置响应状态码为 200
      res.end( `/:id--localhost:${id}`)
    })
    .listen(id, () => {
      console.log(`server is running on port ${id}`)
    })
  res.send({
    method: req.method,
    server: `localhost:${id}`,
  })
})

router.get('/methods/:id', (req, res) => {
  console.log(req.params)
  let id = req.params.id || 98
  http
    .createServer((req, res) => {
      //通过method 就可以了
      if (req.method === 'POST') {
        res.end({
          method: req.method,
          server: `localhost:${id}`,
        })
      } else if (req.method === 'GET') {
        res.end({
          method: req.method,
          server: `localhost:${id}`,
        })
      }
    })
    .listen(id, () => {
      console.log(`server is running on port ${id}`)
    })

  res.send({
    server: `localhost:${id}`,
  })
})

router.get('/full/:id', (req, res) => {
  let id = req.params.id || 98
  console.log(req.params)
  // 创建 HTTP 服务器，并传入回调函数用于处理请求和生成响应
  http
    .createServer((req, res) => {
      const { pathname, query } = url.parse(req.url, true) // 解析请求的 URL，获取路径和查询参数

      if (req.method === 'POST') {
        // 检查请求方法是否为 POST
        if (pathname === '/post') {
          // 检查路径是否为 '/post'
          let data = ''
          req.on('data', chunk => {
            data += chunk // 获取 POST 请求的数据
            console.log(data)
          })
          req.on('end', () => {
            res.setHeader('Content-Type', 'application/json') // 设置响应头的 Content-Type 为 'application/json'
            res.statusCode = 200 // 设置响应状态码为 200
            res.end(data) // 将获取到的数据作为响应体返回
          })
        } else {
          res.setHeader('Content-Type', 'application/json') // 设置响应头的 Content-Type 为 'application/json'
          res.statusCode = 404 // 设置响应状态码为 404
          res.end('Not Found') // 返回 'Not Found' 作为响应体
        }
      } else if (req.method === 'GET') {
        // 检查请求方法是否为 GET
        if (pathname === '/get') {
          // 检查路径是否为 '/get'
          console.log(query.a) // 打印查询参数中的键名为 'a' 的值
          res.end('get success') // 返回 'get success' 作为响应体
        }
      }
    })
    .listen(id, () => {
      console.log('server is running on port ' + id) // 打印服务器启动的信息
    })

  res.send({
    server: `localhost:${id}`,
  })
})

/* 
query参数获取
router.get('/:id', (req, res) => {
  console.log(req.query)
  res.send('get id')
})
动态参数获取
router.get('/:id', (req, res) => {
  console.log(req.params)
  res.send('get id')
})
请求体参数获取
router.post('/create', (req, res) => {
  console.log(req.body)
  res.send('post')
}) */

export default router
